//app.js
import { RequestManager } from './util/requestManager.js'
let requestManager = new RequestManager()

import { DBManager } from './util/dbManager.js'
let dbManager = new DBManager()

import { cloud_evn } from './util/config.js'

App({
  onLaunch: function () {

    //  注册启动云函数功能
    this.private_registerCloud();
    this.private_getQRCodeFileInfo();

    let that = this;
    //  获取系统的基本信息
    let menuButtonObject = wx.getMenuButtonBoundingClientRect();
    wx.getSystemInfo({
      success: function(res) {
        that.globalData.screenH = res.screenHeight;
        that.globalData.screenW = res.screenWidth;
        that.globalData.safeArea_top = res.safeArea.top;

        //
        let statusBarHeight = res.statusBarHeight,
          navTop = menuButtonObject.top,//胶囊按钮与顶部的距离
          navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight) * 2;//导航高度
        that.globalData.navHeight = navHeight;
        that.globalData.navTop = navTop;
        that.globalData.windowHeight = res.windowHeight;
      },
    })

    let mobile = wx.getSystemInfoSync();

    if(mobile.model.indexOf("iPhone X")>=0){
      this.globalData.isIphoneX = true
    }
    //  获取用户的授权信息
    wx.getUserInfo({
      success: function (res) {
        that.saveWXUserInfo(res.userInfo)
      }
    })
    this.getUserOpenIdInfo()
  },
  globalData:{
    screenW:0,
    screenH:0,
    safeArea_top:0,
    isIphoneX:false,

    openid: null,
    session_key:null,
    userInfo: null,
    needReload: true,

    //  分享所用到的全局信息数据
    share_openid: null,
    share_codeFilePath: null,
    share_codeFileId: null
  },
  globalCart:[],
  
  //  全局方法——————————————————————————————————————————
  //  保存用户的授权信息
  saveWXUserInfo:function(userinfo){
    this.globalData.userInfo = userinfo
  },

  //  获取用户的openId信息
  getUserOpenIdInfo:function(){
    let that = this;
    wx.login({
      success(res) {
        var code = res.code;
        requestManager.getUserOpenId(code, function (info) {
          
          that.globalData.openid = info.openid;

          //  测试
          // that.globalData.openid = "o1CGg4o1L0yhAsQORyVm0gIK9ozo";
          
          that.globalData.session_key = info.session_key

          that.getUserCartInfo(info.openid)
        })
      }
    })
  },

  //  获取用户购物车的数据信息
  getUserCartInfo:function(openid){
    var that = this;
    requestManager.getAllCartList(openid, result => {
      that.globalCart = result
    });
  },

  //  注册云开发
  private_registerCloud(){
    wx.cloud.init({
      env: cloud_evn,
      traceUser: true,
    });
  },
  //  云函数获取小程序码的图片路径信息
  private_getQRCodeFileInfo(){
    var that = this;
    wx.cloud.callFunction({
      name: 'getOpenid',
      success: res => {
        let openid = res.result.openid;
        dbManager.getQRCodeFileID(openid, result => {
          if (result != null) {
            that.globalData.share_openid = result.user_id;
            that.globalData.share_codeFileId = result.file_id;
          }
        });
      }
    });
  },
  //  分享给好友
  public_getShareInfo(){
    var path = '/pages/index/index-page?id=' + this.globalData.openid
    var shareObj = {
      title: '因为爱，所以推荐给你',
      path: path
    }
    return shareObj;
  }
})